﻿<%@ WebHandler Language="C#" Class="mingxi" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using KeaoDataBase;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;
using ServiceStack.Redis;
public class mingxi : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.QueryString["uid"];
            string kid = context.Request.QueryString["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("[]");
                return;
            }
            string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid,"cache").Replace(kid,"cache"));
            using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
            {
                if (redis != null)
                {
                    if (redis.ContainsKey(key))
                    {
                        context.Response.Write(redis.GetValue(key));
                        return;
                    }
                }
            }
            string zhanDian = context.Request.QueryString["zhandian"];
            string chengZhongLeiXing = context.Request.QueryString["chengzhongleixing"];
            string kaiShiShiJian = context.Request.QueryString["kaishishijian"];
            string jieShuShiJian = context.Request.QueryString["jieshushijian"];
            string danWeiMingCheng = context.Request.QueryString["danweimingcheng"];
            string wuLiaoMingCheng = context.Request.QueryString["wuliaomingcheng"];
            string xingHao = context.Request.QueryString["xinghao"];
            string chePaiHao = context.Request.QueryString["chepaihao"];
            if (string.IsNullOrEmpty(chengZhongLeiXing) || string.IsNullOrEmpty(zhanDian))
            {
                context.Response.Write("[]");
                return;
            }
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                Dictionary<string, string> orderByCondition = new Dictionary<string, string>();
                string createCondition =  m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, chengZhongLeiXing, "", SearchConditionType.等于);
                if (zhanDian != "—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                }
                if (!string.IsNullOrEmpty(kaiShiShiJian) && !string.IsNullOrEmpty(jieShuShiJian))
                {
                    if (chengZhongLeiXing == "入库物料")
                    {
                        orderByCondition.Add("pizhongshijian", "desc");
                        createCondition += m_DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, kaiShiShiJian, jieShuShiJian, SearchConditionType.between);
                    }
                    else
                    {
                        orderByCondition.Add("maozhongshijian", "desc");
                        createCondition += m_DB.CreateAndCondition("maozhongshijian", DataValueCommonType.日期时间, kaiShiShiJian, jieShuShiJian, SearchConditionType.between);
                    }
                }
                if (!string.IsNullOrEmpty(danWeiMingCheng)&&danWeiMingCheng!="—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("danweimingcheng", DataValueCommonType.字符串, danWeiMingCheng, "", SearchConditionType.等于);
                }
                if (!string.IsNullOrEmpty(wuLiaoMingCheng)&&wuLiaoMingCheng!="—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("wuliaomingcheng", DataValueCommonType.字符串, wuLiaoMingCheng, "", SearchConditionType.等于);
                }
                if (!string.IsNullOrEmpty(xingHao)&&xingHao!="—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("xinghao", DataValueCommonType.字符串, xingHao, "", SearchConditionType.等于);
                }
                if (!string.IsNullOrEmpty(chePaiHao))
                {
                    createCondition += m_DB.CreateAndCondition("chepaihao", DataValueCommonType.字符串, chePaiHao, "", SearchConditionType.like);
                }

                DataTable dtMingXi = m_DB.GetDataTableByCondition("tbl_chengzhong", createCondition, orderByCondition);
                DataTable dtCount = m_DB.GetDataTableByCondition("tbl_chengzhong", "count(id) as cheshu,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang", null, createCondition, null, null);
                if (dtMingXi.Rows.Count > 0)
                {
                    DataRow newRow = dtMingXi.NewRow();
                    newRow["chengzhongbianhao"] = "合计：";
                    newRow["kouzhong"] = dtCount.Rows[0]["kouzhong"];
                    newRow["jingzhong"] = dtCount.Rows[0]["jingzhong"];
                    newRow["chepaihao"] = string.Format("{0}车", dtCount.Rows[0]["cheshu"]);
                    newRow["fahuozhongliang"] = dtCount.Rows[0]["fahuozhongliang"];
                    newRow["jisuanfangliang"] = dtCount.Rows[0]["jisuanfangliang"];
                    dtMingXi.Rows.Add(newRow);
                    string dtJson = JsonConvert.SerializeObject(dtMingXi);
                    using (IRedisClient redis = RedisClientPool.GetRedisClient())
                    {
                        if (redis != null)
                        {
                            redis.SetValue(key, dtJson, TimeSpan.FromMinutes(10.0));
                        }
                    }
                    context.Response.Write(dtJson);
                }
                else
                {
                    context.Response.Write("[]");
                }
            }
        }
        catch(Exception err)
        {
            KeaoUtils.LogTxt(string.Format("查询明细信息异常。{0}", err.Message));
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}